package org.example.mq.producer;

import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.AppContext;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @author wuwenxue
 * @describe mq生产者
 */
@Component
@Slf4j
public class DefaultMqProducer extends MQProducer {

    @Resource
    private ApplicationContext wac;

    @PostConstruct
    private void initPro() {
        super.init();
    }

    public void sendMsg(String message, String topic, String tag) {
        try {
            String routeVersion = AppContext.getThreadLocal("routeVersion");
            if (StringUtils.isEmpty(routeVersion)) {
                send(message, topic, tag);
            } else {
                Properties props = new Properties();
                props.setProperty("routeVersion", routeVersion);
                send(message, topic, tag, props);
            }
        } catch (Exception e) {
            log.info("发送MQ失败，重试继续发送,失败原因 = {}", e.getMessage());
            send(message, topic, tag);
        }
    }

    @PreDestroy
    private void destroySubmitAuthTaskProducer() {
        destroy();
    }
}
